using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;

namespace SP14.ExternalList
{
    [Guid("0226d8d7-f868-499e-ad58-f6b8a4b4dac4")]
    public class ExcelPluginEventReceiver : SPFeatureReceiver
    {
        const string CommandUIExtensionFormat = @"<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location=""Ribbon.List.Actions.Controls._children""><Button Id=""Ribbon.List.Actions.ExternalListToExcelButton"" Sequence=""1000"" TemplateAlias=""o1"" Command=""ExternalListToExcelButton"" Alt=""Export to Excel"" LabelText=""Export to Excel"" ToolTipTitle=""Export to Excel"" ToolTipDescription = ""Analyze items in this list using Microsoft Excel"" Image16by16=""/_layouts/ExternalListExcelPlugin/Excel16.png"" Image32by32=""/_layouts/ExternalListExcelPlugin/Excel32.png"" /></CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers><CommandUIHandler Command =""ExternalListToExcelButton"" CommandAction=""{2}/_vti_bin/ExternalList/owssvr.dll?IQY=1&amp;List={0}&amp;View={1}"" /></CommandUIHandlers></CommandUIExtension>";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            if (web != null)
            {
                foreach (SPList list in web.Lists)
                {
                    if (list.HasExternalDataSource)
                    {
                        AddListAction(list);
                    }
                }
            }
        }

        static void AddListAction(SPList list)
        {
            var action = list.UserCustomActions.Add();
            action.Name = "ExternalListToExcelButton";
            action.Location = "CommandUI.Ribbon";
            action.Sequence = 1000;
            action.Title = "Export to Excel";
            action.Description = "Analyze items in this list using Microsoft Excel";
            string commandAction = string.Format(CommandUIExtensionFormat, list.ID.ToString("B"), list.DefaultView.ID.ToString("B"), list.ParentWebUrl);
            action.CommandUIExtension = commandAction;
            action.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            if (web != null)
            {
                foreach (SPList list in web.Lists)
                {
                    if (list.HasExternalDataSource && list.UserCustomActions.Count > 0)
                    {
                        SPUserCustomAction action = null;
                        foreach (SPUserCustomAction ca in list.UserCustomActions)
                        {
                            if (ca.Name == "ExternalListToExcelButton")
                            {
                                action = ca;
                                break;
                            }
                        }
                        if (action != null)
                        {
                            action.Delete();
                        }
                        //list.UserCustomActions.Clear();
                    }
                }
            }
        }
    }
}
